import { JsonController, Body, Post, UploadedFile,UseInterceptor, Req, Res, Get } from "routing-controllers";
import { JsonResponseMiddlewares } from "../../middleware/JsonResponseMiddlewares";
import { Service, Inject } from "typedi";
import { NewCourseModel, updateCourseVideo,UpdateCourse,againUpdateCourseVideo } from "../../model/NewCourseModel";
import { config } from "../../config";
import { utils } from "../../utils/utils";
import { AdminServiceImpl } from "../../service/impl/AdminServiceImpl";
import { QiNiu } from "../../plugins/qiniu"

@Service()
@JsonController(`${config.apiurl}/admin`)
@UseInterceptor(JsonResponseMiddlewares)
export class AdminController {
	
	@Inject()
	private utils: utils;
	
	@Inject()
	private adminServiceImpl: AdminServiceImpl;
	
	@Inject()
	private qiniu:QiNiu;
	
	/**
	 * 课程的缩略图上传
	 * @param file
	 * @constructor
	 */
	@Post("/upload")
	public async UploadImg(@UploadedFile("file") file: any): Promise<Object> {
		return this.utils.UpLoadFileImg(file)
	}
	
	/**
	 * 发布新课程
	 * @param Course
	 */
	@Post("/newCourse")
	public async newCourse(@Body() Course: NewCourseModel): Promise<Object> {
		return await this.adminServiceImpl.addNewCourse(Course);
	}
	
	/**
	 * 获取所有的课程列表
	 * @param page
	 */
	@Post("/getAllCourse")
	public async getAllCourse(@Body() page: number): Promise<Object> {
		return this.adminServiceImpl.getAllCourse(page)
	}
	
	/**
	 * 修改更新课程信息
	 * @param Course
	 * @constructor
	 */
	@Post("/UpdateCourse")
	public async UpdateCourse(@Body() Course: UpdateCourse): Promise<Object> {
		return await this.adminServiceImpl.UpdateCourse(Course);
	}
	
	/**
	 * 获取课程的详情
	 * @param params
	 */
	@Post("/getCourseInfo")
	public async getCourseInfo(@Body() params: Object): Promise<Object> {
		return this.adminServiceImpl.getCourseInfo(params['id'])
	}
	
	/**
	 * 发布课程所属的视频
	 * @param Course
	 */
	@Post("/updateCourseVideo")
	public async updateCourseVideo(@Body() Course: [ updateCourseVideo ]): Promise<Object> {
		return await this.adminServiceImpl.updateCourseVideo(Course);
	}
	
	@Post("/againUpdateCourseVideo")
	public async againUpdateCourseVideo(@Body() Course: [ againUpdateCourseVideo ]): Promise<Object> {
		return await this.adminServiceImpl.againUpdateCourseVideo(Course);
	}
	
	@Post("/deleteCourseAndVideo")
	public async deleteCourseAndVideo(@Body() params: Object): Promise<Object> {
		return await this.adminServiceImpl.deleteCourseAndVideo(params['id']);
	}
	
	/**
	 * 获取课程下面所属的视频章节
	 * @param params
	 */
	@Post("/getVideoList")
	public async getVideoList(@Body() params: Object): Promise<Object> {
		return await this.adminServiceImpl.getVideoList(params['id']);
	}
	
	
	@Post("/getAllClass")
	public async getAllClass(): Promise<Object> {
		return await this.adminServiceImpl.getAllClass();
	}
	
	@Post("/deleteClassById")
	public async deleteClassById(@Body() params: Object): Promise<Object> {
		return await this.adminServiceImpl.deleteClassById(params['id']);
	}
	
	@Post("/addNewClass")
	public async addNewClass(@Body() params: Object): Promise<Object> {
		return await this.adminServiceImpl.addNewClass(params['title']);
	}
	
	
	@Post("/adminLogin")
	public async adminLogin(@Body() params: Object): Promise<Object> {
		return await this.adminServiceImpl.adminLogin(params);
	}
	
	
	@Post("/token")
	public async Token (@Body() params: Object): Promise<Object> {
		return {
			token: this.qiniu.GenerateToken(),
			hls: this.qiniu.GenerateHlsToken(params)
		};
	}
	
	@Post("/HlsToken")
	public async HlsToken (@Body() params: Object): Promise<Object> {
		return this.qiniu.GenerateHlsToken(params);
	}
	
}